home *** CD-ROM | disk | FTP | other *** search
- #define _Main_Module_
-
- #include <Palettes.h>
- #include <Profiler.h>
- #include "AppGlobals.h"
- #include "AppTypes.h"
- #include "AppConstants.h"
- #include "AppMacros.h"
- #include "DrawGridUtilPr.h"
- #include "TablesUtilPr.h"
- #include "ToolTrapsPr.h"
- #include "NewDialogPr.h"
-
- #include "ShellPr.h"
-
- static void InitToolbox ( void );
- static Boolean InitMemory ( void );
- static WindowRef InitWindow ( void );
- static Boolean SetWindPaletteFromClut ( WindowRef, CTabHandle, short );
- static Boolean InitGrid ( WindowRef );
- static void RandomizeGrid ( GridDataPtr, Boolean );
-
- // main - the program entry/exit point
- void main( void )
- {
- // start up the profiler
- #if __profile__
- if( ProfilerInit( collectDetailed, bestTimeBase, 100, 10 ) != noErr )
- Debugger();
- #endif
-
- InitToolbox();
- {
- short depthMode;
-
- gSaveGDevice = GetMainDevice();
- gSaveDepth = HasDepth( gSaveGDevice, (**((**gSaveGDevice).gdPMap)).pixelSize, 1, 1 );
- if( depthMode = HasDepth( gSaveGDevice, 8, 1, 1 ) )
- SetDepth( gSaveGDevice, depthMode, 1, 1 );
- else
- {
- Alert( k8BitColorsID, nil );
- return;
- }
- }
- #ifndef powerc
- GetCommonTraps();
- #endif
- if( InitMemory() )
- {
- WindowRef theWindow;
- Boolean allGood = true;
-
- while( (theWindow = InitWindow()) && allGood )
- {
- FlushEvents( everyEvent, 0 );
- if( allGood = InitGrid( theWindow ) )
- {
- while( !MButton() )
- calculateNextIteration( theWindow );
-
- DisposeTables( (GridDataPtr)MGetWRefCon( theWindow ) );
- UnlockPixels( MGetGWorldPixMap( ((GridDataPtr)MGetWRefCon( theWindow ))->theGWorld ) );
- DisposePtr( (Ptr)MGetWRefCon( theWindow ) );
- DisposeWindow( theWindow );
- }
- }
- }
- SetDepth( gSaveGDevice, gSaveDepth, 1, 1 );
-
- // shut down the profiler
- #if __profile__
- ProfilerDump( "\pHexCA.prof" );
- ProfilerTerm();
- #endif
- }
-
- // InitToolbox - call to initialize the Mac toolboxes
- static void InitToolbox( void )
- {
- InitGraf( &qd.thePort );
- InitFonts();
- InitWindows();
- InitMenus();
- InitCursor();
- TEInit();
- InitDialogs( 0L );
- GetDateTime( (unsigned long *)&qd.randSeed );
- }
-
- // InitMemory - call to initialize the memory
- static Boolean InitMemory( void )
- {
- if( !(gClut = GetCTable( kClutTableID )) )
- {
- Alert( kNotEnoughMemoryID, nil );
- return( false );
- }
- if( !(gDeskMenu = GetMenu( kAppleMenuID )) )
- {
- Alert( kNotEnoughMemoryID, nil );
- return( false );
- }
- AddResMenu( gDeskMenu, 'DRVR' );
- if( !(gFileMenu = GetMenu( kFileMenuID )) )
- {
- Alert( kNotEnoughMemoryID, nil );
- return( false );
- }
- if( !(gEditMenu = GetMenu( kEditMenuID )) )
- {
- Alert( kNotEnoughMemoryID, nil );
- return( false );
- }
- return( true );
- }
-
- // InitWindow - call to create a window over the entire screen
- static WindowRef InitWindow( void )
- {
- WindowRef theWindow;
- GrafPtr savePort;
- short mBarHeight;
- Rect winRect = qd.screenBits.bounds;
-
- InsertMenu( gDeskMenu, 0 );
- InsertMenu( gFileMenu, 0 );
- InsertMenu( gEditMenu, 0 );
- InvalMenuBar();
-
- mBarHeight = GetMBarHeight();
- MInsetRect( winRect, 4, 4 );
- winRect.top += mBarHeight;
-
- if( !(theWindow = NewCWindow( nil, &winRect, "\p", false, plainDBox, (WindowRef)-1L, false, 0L )) )
- {
- Alert( kNotEnoughMemoryID, nil );
- return( nil );
- }
- GetPort( &savePort );
- SetPort( theWindow );
-
- if( !SetWindPaletteFromClut( theWindow, gClut, 0x0000 ) )
- {
- DisposeWindow( theWindow );
- return( nil );
- }
-
- TextFont( geneva );
- TextSize( 10 );
-
- SetPort( savePort );
-
- return( theWindow );
- }
-
- // SetWindPaletteFromClut - Sets the given window's palette to the given colour table
- static Boolean SetWindPaletteFromClut( WindowRef theWindow, CTabHandle theClut, short tolerence )
- {
- PaletteHandle thePalette = nil;
-
- if( !(thePalette = GetPalette( theWindow )) )
- {
- if( !(thePalette = NewPalette( (**theClut).ctSize + 1, theClut, pmTolerant + pmExplicit, tolerence )) )
- {
- Alert( kNotEnoughMemoryID, nil );
- return( false );
- }
- }
- else
- {
- CTab2Palette( theClut, thePalette, pmTolerant + pmExplicit, tolerence );
- if( !thePalette )
- {
- Alert( kNotEnoughMemoryID, nil );
- return( false );
- }
- }
-
- NSetPalette( theWindow, thePalette, pmAllUpdates );
-
- ActivatePalette( theWindow );
-
- return( true );
- }
-
- // InitGrid - call to initialize the grid for a window
- static Boolean InitGrid( WindowRef theWindow )
- {
- GridDataPtr refCon;
- Boolean seedCenter;
-
- if( !(refCon = (GridDataPtr)NewPtr( sizeof( GridDataRec ) )) )
- {
- Alert( kNotEnoughMemoryID, nil );
- return( false );
- }
- MSetWRefCon( theWindow, (long)refCon );
-
- if( !doNewDialog( refCon, &seedCenter ) )
- return( false );
- ClearMenuBar();
- DrawMenuBar();
- ShowWindow( theWindow );
-
- refCon->theGWorld = (GWorldPtr)theWindow;
-
- switch( refCon->scale )
- {
- case kHexSize:
- refCon->gridHSize = (theWindow->portRect.right - kHexSize) / kHexSize;
- refCon->gridVSize = theWindow->portRect.bottom / (kHexMidPoint + kHexMidPoint / 2 + 1);
- break;
- case kMiniHexSize:
- refCon->gridHSize = (theWindow->portRect.right - kMiniHexSize) / kMiniHexSize;
- refCon->gridVSize = theWindow->portRect.bottom / (kMiniHexMidPoint + kMiniHexMidPoint / 2 + 1);
- break;
- case kTinyHexSize:
- refCon->gridHSize = (theWindow->portRect.right - kTinyHexSize) / kTinyHexSize;
- refCon->gridVSize = theWindow->portRect.bottom / (kTinyHexMidPoint + kTinyHexMidPoint / 2 + 1);
- break;
- }
-
- if( refCon->gridHSize % 2 ) refCon->gridHSize--;
- if( refCon->gridVSize % 2 ) refCon->gridVSize--;
- refCon->gridTotalSize = refCon->gridHSize * refCon->gridVSize;
- refCon->iterationString[0] = 1;
- refCon->iterationString[1] = '0';
-
- LockPixels( MGetGWorldPixMap( refCon->theGWorld ) );
- if( !InitWindowTables( theWindow ) )
- {
- UnlockPixels( MGetGWorldPixMap( refCon->theGWorld ) );
- DisposePtr( (Ptr)refCon );
- Alert( kNotEnoughMemoryID, nil );
- return( false );
- }
- RandomizeGrid( refCon, seedCenter );
- drawHexGrid( theWindow );
- return( true );
- }
-
- // RandomizeGrid - call to randomize the grid
- static void RandomizeGrid( GridDataPtr refCon, Boolean seedCenter )
- {
- if( seedCenter )
- *(refCon->oldGrid + (refCon->gridTotalSize + refCon->gridHSize) / 2) = 1;
- else
- {
- long count = 0;
-
- for( count = 0; count < refCon->gridTotalSize; count++ )
- *(refCon->oldGrid + count) = MRandom() > 16000;
- }
- }